<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 商品卡片 -->
    <el-card class="mt16">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @input="searchCon"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/addGoods')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <!-- 商品表格 -->
      <el-table :data="goodsList" border style="width: 100%" class="mt16">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ new Date(scope.row.add_time * 1000).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              @click="edit(scope.row)"
              type="primary"
              icon="el-icon-edit"
              size="small"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="mt16"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 编辑对话框 -->
      <el-dialog title="添加用户" :visible.sync="dialogEdit">
        <el-form ref="editRef" :model="goodsData">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input
              v-model="goodsData.goods_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="商品价格" prop="goods_price">
            <el-input
              v-model="goodsData.goods_price"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="商品重量" prop="goods_weight">
            <el-input
              v-model="goodsData.goods_weight"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="商品数量" prop="goods_number">
            <el-input
              v-model="goodsData.goods_number"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEdit = false">取 消</el-button>
          <el-button type="primary" @click="editOk">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList, goodsEdit, goodsDelete } from '@/http/api'
export default {
  name: '',
  components: {},
  data() {
    return {
      // 表格数据
      goodsList: [],
      goodsData: [],
      total: 0, //总数据
      queryInfo: {
        query: '', //搜索关键字
        pagenum: 1, //当前页码数
        pagesize: 10 //每页显示几条
      },
      dialogEdit: false //隐藏对话框
    }
  },
  created() {
    this.getGoodsList()
  },
  mounted() {},
  methods: {
    // 删除功能
    del(row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        // 确定删除 调用接口
        .then(() => {
          goodsDelete(row.goods_id).then(() => {
            // 提示
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            // 更新列表
            this.getGoodsList()
          })
        })
        // 取消删除
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑功能
    edit(row) {
      this.dialogEdit = true
      this.goodsData = row
      console.log(row)
    },
    // 编辑完成
    editOk() {
      goodsEdit(this.goodsData).then((res) => {
        console.log(res)
      })
      // 隐藏对话框
      this.dialogEdit = false
      // 刷新列表
      this.getGoodsList()
    },
    // 获取商品列表数据
    async getGoodsList() {
      const res = await getGoodsList(this.queryInfo)
      console.log(res)
      this.goodsList = res.goods
      this.total = res.total
    },
    // 分页功能
    handleSizeChange(val) {
      console.log(val)
      this.queryInfo.pagesize = val
      // 刷新列表
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 搜索功能的防抖
    searchCon: _.debounce(function () {
      this.getGoodsList()
    }, 300)
  }
}
</script>
<style scoped lang="scss"></style>
